package com.dongdongshop.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
//声明切面类
@Aspect
public class LogAspect {
    //日志LoggerFactory
    private final Logger logger = LoggerFactory.getLogger(LogAspect.class);
    //定义切点表达式  切service下所有的类与方法
    @Pointcut("execution(public * com.dongdongshop.service..*.*(..))")
    public void aopLog(){
    }

    //前置通知
    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint) throws  Throwable{
        //获取所有的参数
        Object[] args = joinPoint.getArgs();
        //获取方法名
        Signature signature = joinPoint.getSignature();
        logger.info("正在执行的方法为 : {} , 参数是 : {} ",signature,args);
    }

    //返回后通知
    @AfterReturning(pointcut = "aopLog()",returning = "result")
    public void doAfter(Object result) throws Throwable{
        logger.info("方法执行成功,返回结果为 : {} ",JSONObject.toJSONString(result));
    }
}